<!--行政区划-->
<template>
  <div class="app-container">
    <!--列表-->
    <div class="table-container flex-main">
      <div class="table-container-header ">
        <table-toolbar :create="false" :remove="false" @refresh="handleFilter">
          <template>
            <el-button icon="el-icon-refresh" plain @click="handleSyncCacheRegion">刷新缓存</el-button>
          </template>
          <template slot="rightTool">
            <search v-model="listQuery.regionName" placeholder="请输入区域名称" @search="handleFilter" />
            <export-drop-down class-name="region" :actions="['exportRegionData','getRegionExportProcessStatus']" :list-query="listQuery" title="行政区划" :async="true" />
          </template></table-toolbar>
      </div>
      <el-table
        ref="multipleTable"
        :data="list"
        :load="load"
        :tree-props="{hasChildren: 'hasChildren'}"
        header-cell-class-name="table-header"
        fit
        height="100%"
        highlight-current-row
        lazy
        row-key="id"
        @sort-change="sortChange">
        <el-table-column
          label="区域名称"
          prop="name"
          sortable
          min-width="300"
          header-align="center">
          <template slot-scope="scope">
            ({{ scope.row.adCode }})&nbsp;&nbsp;
            <span class="color-primary">{{ scope.row.name }}</span>
          </template>
        </el-table-column>
        <el-table-column label="简拼" prop="firstLetter" sortable width="80" align="center" />
        <el-table-column label="区域类型" prop="level" width="120" align="center">
          <template slot-scope="scope">
            <span v-if="scope.row.level=='country'">国家</span>
            <span v-if="scope.row.level=='province'">省份</span>
            <span v-if="scope.row.level=='city'">市</span>
            <span v-if="scope.row.level=='district'">区县</span>
            <span v-if="scope.row.level=='street'">街道</span>
          </template>
        </el-table-column>
        <el-table-column label="区域编码" prop="adCode" sortable width="120" align="center" />
      </el-table>
    </div>
    <div class="pagination_handler">
      <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="fetchData" />
    </div>
  </div>
</template>

<script>

export default {
  components: { },

  data() {
    return {
      list: [],
      total: 0,
      listQuery: {
        page: 1,
        limit: this.common.PAGE_LIMIT_DEFAULT,
        pid: 100000,
        regionName: undefined,
        sort: '+adCode'
      },

      multipleSelectionRecords: []
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    async handleSyncCacheRegion() {
      await this.$store.dispatch('syncCacheRegion')
      this.$modal.msgSuccess('缓存成功')
    },

    load(tree, treeNode, resolve) {
      this.$store.dispatch('regionLoadByPid', tree.id).then(response => {
        response.filter((item) => {
          item.isParent = true
          item.hasChildren = true
          return true
        })
        resolve(response)
      }).catch(() => {})
    },

    async fetchData() {
      const response = await this.$store.dispatch('regionPage', this.listQuery)
      this.list = response.records
      this.list.filter((item) => {
        item.isParent = true
        item.hasChildren = true
        return true
      })
      this.total = response.total
    },
    // 固定写法
    sortChange(data) {
      const { prop, order } = data
      if (prop) {
        if (order === 'ascending') {
          this.listQuery.sort = '+' + prop
        } else {
          this.listQuery.sort = '-' + prop
        }
      } else {
        this.listQuery.sort = '+adCode'
      }
      this.handleFilter()
    },
    /**
     * 搜索
     */
    handleFilter() {
      this.listQuery = {
        ...this.listQuery,
        page: 1
      }
      this.fetchData()
    },
    /**
     * 重置
     */
    handleReset() {
      this.listQuery = {
        ...this.listQuery,
        page: 1,
        regionName: '',
        sort: '+adCode'
      }
      this.fetchData()
    }
  }
}
</script>
<style lang="scss" scoped>

</style>
